﻿import orderControlUI from "../orderControlUI"

@Component
export default class OrderSwitch extends Script {

    /**是先手还是后手（我希望他是一种状态） */
    private isFirst = true;
    /**是否在播放动画 */
    private curtween: Tween<any> = null;
    /**动画时间 */
    private animTime: number = 500;

    private orderUI: orderControlUI = UIService.show(orderControlUI);
    /** 当脚本被实例后，会在第一帧更新前调用此函数 */
    protected onStart(): void {
        this.firstOrSecond();

    }

    /**
     * 周期函数 每帧执行
     * 此函数执行需要将this.useUpdate赋值为true
     * @param dt 当前帧与上一帧的延迟 / 秒
     */
    protected onUpdate(dt: number): void {

    }

    /** 脚本被销毁时最后一帧执行完调用此函数 */
    protected onDestroy(): void {

    }
    private firstOrSecond() {
        InputUtil.onKeyDown(Keys.J, () => {
            if (this.curtween) return
            if (this.isFirst == true) {

                this.switchSecond();
                this.isFirst = false;
                console.log(this.isFirst);
            } else if (this.isFirst == false) {
                this.switchFirst();
                this.isFirst = true;
            }

        });
    }

    /**切换为后手 */
    public switchSecond() {
        let starPos = this.orderUI.imageCanvas.position.clone();
        let endPos = new Vector2(this.orderUI.imageCanvas.position.x, this.orderUI.imageCanvas.position.y + 101);
        this.curtween = new mw.Tween({ pos: starPos.clone() }).to({ pos: endPos }, this.animTime)
            .onUpdate((obj) => {

                this.orderUI.imageCanvas.position = obj.pos;
            })
            .onComplete(() => {
                this.curtween = null;


            })
            .start();
    }
    public switchFirst() {
        let starPos = this.orderUI.imageCanvas.position.clone();
        let endPos = new Vector2(this.orderUI.imageCanvas.position.x, this.orderUI.imageCanvas.position.y - 101);
        this.curtween = new mw.Tween({ pos: starPos.clone() }).to({ pos: endPos }, this.animTime)
            .onUpdate((obj) => {
                this.orderUI.imageCanvas.position = obj.pos;
            })
            .onComplete(() => {
                this.curtween = null;


            })
            .start();
    }
}